package com.qcby.controller;

import com.qcby.entity.*;
import com.qcby.entity.Query.ShopQuery;
import com.qcby.entity.Result;
import com.qcby.entity.Shop;
import com.qcby.entity.User;
import com.qcby.service.ShopService;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Console;
import java.util.List;

@RequestMapping("/shop")
@RestController
public class ShopController {
    @Autowired
    private ShopService shopService;
    @RequestMapping("/tag")
    public  Result shopTag(){
        return Result.success(shopService.findTag());
    }
    @RequestMapping("/list")
    public Result list(ShopQuery shopQuery){
        PageResult<Shop> list = shopService.list(shopQuery);
        return Result.success(list);
    }
    @RequestMapping("deleteById")
    public Result delete(Integer id){
        shopService.deleteById(id);
        return Result.success();
    }
    @RequestMapping("/detail")
    public Result findById(Integer id){
        Shop shop = shopService.findById(id);
        return Result.success(shop);
    }
    @RequestMapping("/userList")
    public Result findUserList(){
        List<User> userList = shopService.findUserList();
        return Result.success(userList);
    }
    @RequestMapping("/add")
    public Result add(Shop shop){
        System.out.println(shop+"shop店铺是什么");
        shopService.add(shop);
        return Result.success();
    }
    @RequestMapping("/update")
    public Result updateById(Shop shop){
        System.out.println(shop+"店铺的信息");
        shopService.updateById(shop);
        return Result.success();
    }

    @ResponseBody
    @PostMapping("/rating")
    public Result<Integer> rating(        @Param("shopId") Integer shopId,
                                          @Param("userId") Integer userId,
                                          @Param("rating") Integer rating){
        return shopService.rating(shopId,userId,rating);
    }
    @ResponseBody
    @PostMapping("/ratingCount")
    public Result<Integer> ratingCount(@Param("shopId") Integer shopId,@Param("userId") Integer userId){
        return shopService.ratingCount(shopId,userId);
    }
    @ResponseBody
    @PostMapping("/getRecent100RatingsAverage")
    public Result<Double> getRecent100RatingsAverage(Integer shopId){
        return shopService.getRecent100RatingsAverage(shopId);
    }    @ResponseBody
    @PostMapping("/updateShopRatingStatus")
    public Result<Integer> updateShopRatingStatus(){
        return shopService.updateShopRatingStatus();
    }
    @ResponseBody
    @PostMapping("/addReview")
    public Result<Integer> addReview(@RequestBody Review review){
        return shopService.addReview(review);
    }
    @ResponseBody
    @PostMapping("/likeReview")
    public Result<Integer> likeReview(@Param("targetId") Integer targetId,
                                      @Param("userId") Integer userId,
                                      @Param("target_type")String target_type){
        Integer is=shopService.cancelReviewLike(targetId,userId,target_type);
        if(is>0) {
            shopService.likeDelete(targetId, userId,target_type);
            Integer count = (shopService.likeCount(targetId, target_type)).getData();
            shopService.syncReviewLikeCount(count);
            return Result.success(0);
        }
        else if (is==0){
            shopService.likeReview(targetId, userId);
            Integer count = (shopService.likeCount(targetId, target_type)).getData();
            shopService.syncReviewLikeCount(count);
            return Result.success(1);
        }
            else {
            return Result.success(2);
        }

    }
    @ResponseBody
    @PostMapping("/getReplies")
    public Result<List<Review>> getReplies(Integer reviewId){
        return shopService.getReplies(reviewId);
    }

    @RequestMapping("/findAll1")
    public Result findAll(Shop shop){
        List<Shop> shops = shopService.findAll1();
        return Result.success(shops);
    }
/*    @ResponseBody
    @PostMapping("/getShopReviews")
    public Result<List<Review>> getShopReviews(Integer shopId){
        List<Review> reviews=shopService.getReviewsByShopId(shopId);
        return Result.success(reviews);
    }*/
    @PostMapping("/getShopReviews")
    @ResponseBody
    public Result<PageResult<Review>> getShopReviews(
            @RequestParam Integer shopId,
            @RequestParam Integer page,
            @RequestParam Integer pageSize) {
        PageResult<Review> result = shopService.getShopReviews(shopId, page, pageSize);
        System.out.println(result);
        return Result.success(result);
    }
    @RequestMapping("/getShopsByUserName")
    public Result getShopsByUserName(String userName){
        List<Shop> shops = shopService.getShopsByUserName(userName);
        return Result.success(shops);
    }
    @RequestMapping("/getShopsByAduit")

    public Result getShopsByAduit(Integer id){
        Shop shops = shopService.getShopsByAduit(id);
        return Result.success(shops);
    }

}
